const app = getApp()

Component({
  data: {
    showModal: false,
    selected: 0,
    navHeight: app.globalData.navHeight,
    qrcode: '',
    cartNum: 0,
    static_root: App.static_root, //静态资源变量
    color: "#999999",
    selectedColor: "#83DAD1",
    gradeId: "",
    right_item: {
      pagePath: "pages/xgPreventionGuide/index",
      iconPath: '/images/cate.png',
      selectedIconPath: "/images/cate-active.png",
      text: "防疫"
    },
    list: [],
    list_r: [{
        pagePath: "pages/index/index",
        iconPath: "/images/home.png",
        selectedIconPath: "/images/home-active.png",
        text: "天气"
      },
      {
        pagePath: "pages/works/index",
        text: "功能",
        iconPath: "/images/up.png",
        selectedIconPath: "/images/up-active.png"
      },
      {
        pagePath: "pages/user/index",
        text: "简介",
        iconPath: "/images/user.png",
        selectedIconPath: "/images/user-active.png"
      }
    ],
  },
  attached() {

  },
  lifetimes: {
    ready: function () {
      this.getGradeId();
      // 页面被展示
      // this.getCartNum();
    }
  },
  methods: {
    /**
     * 分享当前页面
     */
    onShareAppMessage() {
      return {
        title: '',
        path: "/pages/index/index?" + App.getShareUrlParams(),
      };
    },
    switchTab(e) {
      const data = e.currentTarget.dataset
      const url = data.path
      app.navigationTo(url);
    },
    getGradeId() {
      this.setData({
        gradeId: wx.getStorageSync('gradeId') || 0
      })
      if(this.data.gradeId < 3) {
        this.setData({
          list: this.data.list_r
        })
      } else {
        this.setData({
          list: this.data.list_g
        })
      }
      
    },
    //获取购物车数量
    getCartNum() {
      let _this = this;
      if(_this.data.gradeId >= 3) {
        return
      }
      if (App.checkIsLogin()) {
        App._post_form('cart/getCartNum', [], function (result) {
          _this.setData({
            cartNum: result.data
          });

        })
      }

    },
    jumpToQrcode(e) {

      let _this = this;
      _this.setData({
        'qrcode': App.globalData.shareImageUrl
      });
      wx.showLoading({
        title: '加载中',
      });
      if (_this.data.qrcode) {
        _this.buttonTap();
        wx.hideLoading();
      } else {
        App._get('user.dealer.qrcode/poster', {}, function (result) {
          App.globalData.shareImageUrl = result.data.qrcode
          _this.setData({
            'qrcode': result.data.qrcode
          });
          _this.buttonTap();

        }, null, function () {
          wx.hideLoading();
        });

      }

    },
    saveImg: function () {
      let _this = this;
      wx.downloadFile({
        url: _this.data.qrcode,
        success(res) {
          wx.hideLoading();
          wx.saveImageToPhotosAlbum({
            filePath: res.tempFilePath,
            success(data) {
              wx.showToast({
                title: '保存成功',
                icon: 'success',
                duration: 2000
              });
            },
            fail(err) {
              if (err.errMsg === 'saveImageToPhotosAlbum:fail auth deny') {
                wx.showToast({
                  title: "请允许访问相册后重试",
                  icon: "none",
                  duration: 1000
                });
                setTimeout(() => {
                  wx.openSetting();
                }, 1000);
              }
            },
            complete(res) {
              // wx.hideLoading();
            }
          })
        }
      })
    },
    /**
     * 显示弹窗
     */
    buttonTap: function () {
      this.setData({
        showModal: true
      })
    },
    //防止冒泡
    preventTouchMove: function () {},
    //关闭
    closeShare: function () {
      this.setData({
        showModal: false
      })
    }
  }
})